package net.lbtech.service;

import net.lbtech.util.CommonUtil;
import net.lbtech.util.TimeUtil;
import net.lbtech.util.WifiDataFormatUtil;
import net.lbtech.util.db.DbBuilder;
import net.lbtech.util.exception.GetIaqConfigException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Created by Administrator on 2016/12/2 0002.
 */
public class GetIaqConfig {
    protected static Logger logger = LoggerFactory.getLogger(GetIaqConfig.class);

    //更新空气检测仪设置
    public static byte[] updataIaqConfig(String mac){
        byte[] pm25_alarm = new byte[2];
        byte[] formaldehyde_alam = new byte[2];
        byte is_voice = 0;
        //获取数据库空气检测仪配置
        Map<String, Integer> configMap = selectIaqConfig(mac);
        //pm2.5警戒值
        System.out.println(configMap.get("city"));
        pm25_alarm[1] = (byte) configMap.get("pm25_alarm").intValue();
        //甲醛警戒值
        formaldehyde_alam[1] = (byte) configMap.get("formaldehyde_alam").intValue();
        //蜂鸣器
        is_voice = (byte) configMap.get("is_voice").intValue();
        //成功返回
        return WifiDataFormatUtil.iaqConfig(TimeUtil.getTime(), pm25_alarm, formaldehyde_alam, is_voice);
    }

    //查询数据库空气检测仪配置
    public static Map selectIaqConfig(String mac) {
        Map<String, Object> hm = DbBuilder.getFirstRowMap("select * from iaq_instrument_config where mac=?",mac);
        System.out.println(hm);
        return hm;
    }

    //拼接mac地址
    public static String getMacString(byte[] data){
        String mac = "";
        for (int i = 13; i < 18; i++) {
            mac=mac+ CommonUtil.toHex(data[i]);
        }
        System.out.println(mac);
        return mac;
    }

    //获取配置并返回20byte数组
    public static byte[] getIaqConfig(byte[] data) throws GetIaqConfigException {
        String mac = getMacString(data);
        return updataIaqConfig(mac);
    }

}
